package com.sunnypower.rushfitapp;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.sunnypower.rushfitapp.Coachpart.CourseListAdapter;
import com.sunnypower.rushfitapp.entity.VReservecourse;
import com.sunnypower.rushfitapp.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CourseActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText tv_find_course;
    private EditText tv_find_coach;
    private Button btn_find_course;
    private List<VReservecourse> mCourseBeanList;
    private CourseListAdapter adapter;
    private ListView lv_course;

    private String name;
    private String coursename;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        coursename = intent.getStringExtra("coursename");
        uid = intent.getStringExtra("uid");

        mCourseBeanList = new ArrayList<>();
        init();
        try {
            iniCourseData();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        System.out.println("finanl:" + mCourseBeanList.toString());
        lv_course = findViewById(R.id.lv_course);
        adapter = new CourseListAdapter(CourseActivity.this, mCourseBeanList);
        lv_course.setAdapter(adapter);
        lv_course.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final VReservecourse course = mCourseBeanList.get(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(CourseActivity.this);
                builder.setTitle("确定预约" + course.getName() + "的" + course.getCoursename() + "课程？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/ordercourse2app?uid="+uid+"&rid="+course.getRid());
                        System.out.println(data);
                        Toast toast = Toast.makeText(getApplicationContext(), "成功预约" + course.getName() + "的" + course.getCoursename() + "的课程", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
                        toast.show();
                        mCourseBeanList.remove(course);
                        adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", null);
                builder.create().show();
            }
        });
    }

    private void init() {
        tv_find_course = findViewById(R.id.tv_find_course);
        tv_find_coach = findViewById(R.id.tv_find_coach);
        btn_find_course = findViewById(R.id.btn_find_course);
        btn_find_course.setOnClickListener(this);
    }


    private void iniCourseData() throws JSONException {
        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/FindFitReservecourse2app?name=" + name + "&coursename=" + coursename);
        System.out.println(data);
        //解析json数据
        System.out.println("responseData：" + data);
        JSONArray CourseList = null;
        try {
            CourseList = new JSONArray(data);
            System.out.println("CourseList：" + CourseList);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < CourseList.length(); i++) {
            JSONObject jsoncourse = CourseList.getJSONObject(i);
            VReservecourse bean = new VReservecourse();
            bean.setName(jsoncourse.getString("name"));
            bean.setCoursename(jsoncourse.getString("coursename"));
            bean.setTime(jsoncourse.getString("time"));
            bean.setRate(jsoncourse.getString("rate"));
            bean.setRid(jsoncourse.getInt("rid"));
            mCourseBeanList.add(bean);
        }
    }

    @Override
    public void onClick(View v) {
        Toast toast;
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_find_course:
                String findcourse = String.valueOf(tv_find_course.getText());
                String findcoach = String.valueOf(tv_find_coach.getText());
                intent = new Intent(CourseActivity.this, CourseActivity.class);
                intent.putExtra("coursename", findcourse);
                intent.putExtra("name", findcoach);
                intent.putExtra("uid", uid);
                startActivity(intent);
                finish();
                break;
        }
    }

    public Toast onCardClick(Context context, CharSequence text, int duration) {
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
        return toast;
    }
}
